{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-07-31T02:01:29.574741Z",
     "start_time": "2025-07-31T02:01:28.446691Z"
    }
   },
   "source": "import pandas as pd\n",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-31T02:02:19.794208Z",
     "start_time": "2025-07-31T02:02:19.787354Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# series的创建\n",
    "s = pd.Series([1, 2, 3, 4])\n",
    "print(s)"
   ],
   "id": "871eca441fa251b4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    1\n",
      "1    2\n",
      "2    3\n",
      "3    4\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-31T02:09:24.911853Z",
     "start_time": "2025-07-31T02:09:24.904061Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# Series 自定义索引 并给这堆数据进行命名\n",
    "s = pd.Series([1, 2, 3, 4], index=['A', 'B', 'C', 'A'], name='Month')\n",
    "print(s)"
   ],
   "id": "5e359bf91f5d0279",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A    1\n",
      "B    2\n",
      "C    3\n",
      "A    4\n",
      "Name: Month, dtype: int64\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-31T02:13:02.761844Z",
     "start_time": "2025-07-31T02:13:02.753950Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 以字典的形式进行创建series对象\n",
    "s = pd.Series({'a':1, 'b': 1, 'c': 1})\n",
    "print(s)\n",
    "print('-' * 50)\n",
    "# 基于一个series创建一个新的对象\n",
    "s1 = pd.Series(s, ['a', 'b'])\n",
    "print(s1)"
   ],
   "id": "f44e6206bf8bc3a2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    1\n",
      "b    1\n",
      "c    1\n",
      "dtype: int64\n",
      "--------------------------------------------------\n",
      "a    1\n",
      "b    1\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-31T02:56:42.874752Z",
     "start_time": "2025-07-31T02:56:42.852990Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\"\"\"\n",
    "学习Series对象的常用属性\n",
    "\"\"\"\n",
    "s = pd.Series([1, 2, 3, 4], index=['A', 'B', 'C', 'A'], name='Month')\n",
    "print(s.index)\n",
    "print(s.values)\n",
    "print(s.shape)\n",
    "print(s.ndim)\n",
    "print(s.size)\n",
    "print(s.name)"
   ],
   "id": "b110994e3f518a6f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['A', 'B', 'C', 'A'], dtype='object')\n",
      "[1 2 3 4]\n",
      "(4,)\n",
      "1\n",
      "4\n",
      "Month\n",
      "A    1\n",
      "A    4\n",
      "Name: Month, dtype: int64\n"
     ]
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-31T04:04:29.802Z",
     "start_time": "2025-07-31T04:04:29.790399Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 获取series中的元素\n",
    "s = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])\n",
    "# print(s)\n",
    "print('----')\n",
    "# print(s[0:3])\n",
    "print('----')\n",
    "print(s.loc['a':'c']) # 显式索引 左闭右闭\n",
    "print(s.iloc[0:2]) # 隐式索引 左闭右开\n",
    "print(s.at['a']) # 显式 at 仅能获取单个元素\n",
    "print(s.iat['a']) # 隐式 iat: integer at 仅能获取单个元素"
   ],
   "id": "b2da40ec58194f43",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "----\n",
      "----\n",
      "a    1\n",
      "b    2\n",
      "c    3\n",
      "dtype: int64\n",
      "a    1\n",
      "b    2\n",
      "dtype: int64\n",
      "1\n"
     ]
    }
   ],
   "execution_count": 41
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-31T05:26:26.811012Z",
     "start_time": "2025-07-31T05:26:26.789063Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 访问数据的快捷方式，pandas\n",
    "s = pd.Series([1, 2, 3])\n",
    "print(s[0])"
   ],
   "id": "82ad3c1fa668b732",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "1\n"
     ]
    }
   ],
   "execution_count": 44
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-31T05:53:32.794130Z",
     "start_time": "2025-07-31T05:53:32.775595Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 布尔索引的方式\n",
    "s = pd.Series([1, 2 ,3 ,4, 5, 6, 7])\n",
    "print(s[s < 2])\n",
    "print('----;')\n",
    "print(s.head()) #  默认查看series中前5行的数据\n",
    "print(s.head(2)) #  默认查看series中前2行的数据\n",
    "print(s.tail(2)) #  默认查看series中后2行的数据"
   ],
   "id": "654788faffe0fbf1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    1\n",
      "dtype: int64\n",
      "----;\n",
      "0    1\n",
      "1    2\n",
      "2    3\n",
      "3    4\n",
      "4    5\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 48
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
